package com.sp.scmdb.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.MonitorEmpPositionBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.service.MonitorService;

@Controller("monitorEmpPositionAction")
@Scope("prototype")
public class MonitorEmpPositionAction extends BaseAction<MonitorEmpPositionBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(MonitorEmpPositionAction.class);
	
	private MonitorService monitorService;
	
	private String result;
	
	private String tag;

	/**
	 * 卡号定位
	 */
	public String searchByTag() {
		try {
			UserBean user = (UserBean)session.get("user");
			
			List<MonitorEmpPositionBean> list = monitorService.searchByTag(tag, "1", user);			
			
			// 将entity转换成json
			JSONArray jo = JSONArray.fromObject(list);
			result = jo.toString();
			
			return SUCCESS;			
		} catch (Exception e) {
			log.error("init reader failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	/**
	 * 姓名定位
	 */
	public String searchByName() {
		try {
			UserBean user = (UserBean)session.get("user");
			
			String name = request.getParameter("name");
			 name = new String(name.getBytes("ISO-8859-1"));
			List<MonitorEmpPositionBean> list = monitorService.searchByTag(name, "2", user);	
			
			// 将entity转换成json
			JSONArray jo = JSONArray.fromObject(list);
			result = jo.toString();
			
			return SUCCESS;
		} catch (Exception e) {
			log.error("init reader failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	/**
	 * 工号定位
	 */
	public String searchByWorkID() {
		try {
			UserBean user = (UserBean)session.get("user");
			
			String workID = request.getParameter("noEmp");
			List<MonitorEmpPositionBean> list = monitorService.searchByTag(workID, "3", user);
			
			// 将entity转换成json
			JSONArray jo = JSONArray.fromObject(list);
			result = jo.toString();
			
			return SUCCESS;			
		} catch (Exception e) {
			log.error("init reader failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	@Autowired
	public void setMonitorService(MonitorService monitorService) {
		this.monitorService = monitorService;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}
	
}
